package cn.itsy.web.controller;

import cn.itsy.dao.Pactextdao;
import cn.itsy.dao.Pactypedao;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.Pactext;
import cn.itsy.pojo.Pactext_type;
import cn.itsy.pojo.Pactype;
import cn.itsy.pojo.Page;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("pactext")
public class PactextController {
    @Resource
    private Pactypedao pad;
    @Resource
    private Pactextdao pd;
    @Resource
    private Pagedao pagedao;

    @RequestMapping("list")
    public String list(String pid, Model m, Pactext_type p, String currentPage) {
        Page page = new Page();
        String sql = "select p.*,pa.ctname from pactext p,pactype pa where p.pactype_id=pa.id and p.pactype_id='" + pid + "'";
        if (p.getCno() != null && !p.getCno().equals("")) {
            sql += " and p.cno = '" + p.getCno() + "'";
        }
        if (p.getCname() != null && !p.getCname().equals("")) {
            sql += " and p.cname like '%" + p.getCname() + "%'";
        }
        Page pa = pagedao.pactextfy(sql, currentPage, page);
        m.addAttribute("pa", pa);
        m.addAttribute("pid", pid);
        return "/pactext";
    }

    @RequestMapping("toadd")
    public String toadd(Model model, String pid) {
        String sql = "select * from pactype where id='" + pid + "'";
        List<Pactype> idli = pad.list(sql);
        model.addAttribute("idli", idli);
        return "/pactextadd";
    }

    @RequestMapping("add")
    public String add(MultipartFile file, Pactext p, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filename = file.getOriginalFilename();
        String path = request.getServletContext().getRealPath("/upload/");
        File parent = new File(path);
        if (!parent.exists())
            parent.mkdirs();
        File f = new File(path, filename);
        file.transferTo(f);
        p.setCpath(filename);
        String sql = "insert into pactext values(null,'" + p.getCno() + "','" + p.getCname() + "','" + p.getPactypeId() + "','" + p.getCpath() + "','" + p.getCdesc() + "','" + p.getStatus() + "')";
        pd.add(sql);
        return "redirect:list?pid=" + p.getPactypeId() + "";
    }

    @RequestMapping("toupdate")
    public String toupdate(Model model, String id) {
        String sql = "select * from pactext where id='" + id + "'";
        String sql1 = "select * from pactype";
        List<Pactype> dl = pad.list(sql1);
        List<Pactext_type> list = pd.list(sql);
        model.addAttribute("list", list);
        model.addAttribute("dl", dl);
        return "/pactextupdate";
    }

    @RequestMapping("delete")
    public String delete(String id, String pid) {
        String sql = "delete from pactext where id = '" + id + "'";
        pd.delete(sql);
        return "redirect:list?pid=" + pid + "";
    }

    @RequestMapping("update")
    public String update(MultipartFile file, Pactext p, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filename = file.getOriginalFilename();
        String path = request.getServletContext().getRealPath("/upload/");
        File parent = new File(path);
        if (!parent.exists())
            parent.mkdirs();
        File f = new File(path, filename);
        file.transferTo(f);
        p.setCpath(filename);
        String sql = "update pactext set cno='" + p.getCno() + "',cname='" + p.getCname() + "',pactype_id='" + p.getPactypeId() + "',cpath='" + p.getCpath() + "',cdesc='" + p.getCdesc() + "',status='" + p.getStatus() + "' where id='" + p.getId() + "'";
        pd.update(sql);
        return "redirect:list?pid=" + p.getPactypeId() + "";
    }

    @RequestMapping("xz")
    public String xz(String filename, String pid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String path = request.getServletContext().getRealPath("/");// 获取项目路径
        String parent = new File(path).getParent();// 获取项目的父级目录的路径就是webapps的路径
        path = parent + "/maven-project/upload/" + filename;// 文件的路径
        File file = new File(path);// 对应要下载的文件
        // 获取文件的mimi类型
        String mt = request.getServletContext().getMimeType(filename);
        response.setContentType(mt);// 设置响应的文件类型
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);//设置文件名
        // 获取文件输入流，以及服务器响应流
        FileInputStream in = new FileInputStream(file);
        ServletOutputStream out = response.getOutputStream();
        IOUtils.copy(in, out);//写回浏览器
        in.close();
        out.close();

        return "redirect:list?pid=" + pid + "";
    }
}
